/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elsimulatorjava;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author corey
 */
public class FloorDialog extends JDialog {
    private JTable tableWidget;
    private JScrollPane tableView;
    private JButton clearB, submitB, fillB;
    private String[] tableColumns = {"Floor Name"};
    
    private Floor[] floors;
    
    private int numOfFloors;
    
    FloorDialog(short nF){
        //floors = new Floor[nF];
        this.numOfFloors = nF;
        this.setTitle("Floor set up");
        String[][] tableData = new String[this.numOfFloors][1];
        this.setModal(true);

        for (int i = 0; i < this.numOfFloors; i++){
            tableData[i][0] = "Floor " + i;
        }
        
        this.clearB = new JButton();
        this.clearB.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                for (int i = 0; i < numOfFloors; i++){
                    tableWidget.setValueAt("", i, 0);
                }
                
            }
        }); 
        this.submitB = new JButton();
        this.submitB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                boolean close = true;
                floors = new Floor[numOfFloors];
        
                for (int i = 0; i < numOfFloors; i++){
                    String floorName = tableWidget.getValueAt(i, 0).toString();
                    if (!floorName.isEmpty()){
                        floors[i] = new Floor(floorName);
                    } else {
                        close = false;
                    }
                }
                
                if (close){
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(new JDialog(), "All floors must be named.", "Warning", JOptionPane.WARNING_MESSAGE);
                }
            }
        }); 
        this.fillB = new JButton();
        this.fillB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                for (int i = 0; i < numOfFloors; i++){
                    tableWidget.setValueAt("Floor " + i, i, 0);
                }
            }
        }); 
        
        this.fillB.setText("Fill");
        this.clearB.setText("Clear");
        this.submitB.setText("Set Floors");
        //this.submitB.setDefault(true);
        
        this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
        GridLayout buttonGridLayout = new GridLayout(1,3);
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(buttonGridLayout);
        buttonPanel.setSize(350, 280);
        buttonPanel.add(clearB);
        buttonPanel.add(fillB);
        buttonPanel.add(submitB);
        
        
        this.tableWidget = new JTable(tableData, tableColumns);
        this.tableWidget.getTableHeader().setResizingAllowed(false);
        this.tableWidget.getTableHeader().setReorderingAllowed(false);
        tableView = new JScrollPane(tableWidget);
        tableWidget.setFillsViewportHeight(true);
    
        this.add(tableView);
        this.add(buttonPanel);
        this.setSize(350,300);
    }
    
    Floor[] getFloors(){
        return floors;
    }
    
    void setFloors(ArrayList<Floor> f){
        /*this.floors = f;
        for (int i = 0; i < this.floors.size(); i++){
            tableWidget.setItem(i, 0, new QTableWidgetItem(this.floors.get(i).getFloorName()));
        }*/
    }

}
